<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Specs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SpecsController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $data= DB::table('specs')->orderBy('id','desc')->paginate(10);
       return $this->success($data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $data=Specs::create($request->all());
        $res['specs_name']=$data->specs_name;
        $res['products_id']=$data->products_id;
        $res['id']=$data->id;
        if($data) return $this->success($res);
        return $this->apiError();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        if(DB::table('specs')->where('id',$id)->update($request->all())) return $this->success();
        return $this->apiError();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table('specs')->delete($id);
        DB::table('specs_value')->where('specs_id',$id)->delete();
         return $this->success();

    }
}
